/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author chanon
 */
public class FolderZiperAction extends org.apache.struts.action.Action {
   
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            File inFolder=new File("c:/public");

             File outFolder=new File("c:/Out.zip");
             ZipOutputStream out = new ZipOutputStream(new
             BufferedOutputStream(new FileOutputStream(outFolder)));
             BufferedInputStream in = null;
             byte[] data    = new byte[1000];
             String files[] = inFolder.list();
             for (int i=0; i<files.length; i++)
              {
              in = new BufferedInputStream(new FileInputStream
              (inFolder.getPath() + "/" + files[i]), 1000);                 
              out.putNextEntry(new ZipEntry(files[i]));
              int count;
              while((count = in.read(data,0,1000)) != -1)
              {
                   out.write(data, 0, count);
                  }
              out.closeEntry();
              }
              out.flush();
              out.close();
       return mapping.findForward(SUCCESS);
    }
}